#!/usr/bin/perl

$clientID = $ARGV[0];
@array = ();
$a = 626;

#Get current date
#Courtesy of http://www.tutorialspoint.com/perl/perl_localtime.htm
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon += 1;
$year += 1900;

#Open invoices.txt to retrieve client invoices
open($file, "<", "../Clients/info/invoices.txt") or die(exit(4));

while ($line = <$file>) {   # checks all lines in file

        if($line =~ /\s*$clientID\s*                #Client id, must be found, set to $1
                   \|\s*                              #will not match if anything but whitespaces present the second column
                   \|\s*(\d{4}-\d{1,2}-\d{1,2})?\s*   #Invoice issue date YYYY-MM-DD
                   \|\s*([^|\s]*)\s*                  #Invoice number (0 or more characters that are not | or whitespace
                   \|\s*(\d{4}-\d{1,2}-\d{1,2})?\s*     #Invoice date YYYY-MM-DD
                   \|\s*([+-]?[\d]*[.]?[\d]{2})[\s\d]*  #price
                   \|\s*(.*)                            #Path to Invoice file
                   (\#.*)?$                             #End Comments
                   /x) {
                if($1) { $start[$a] = $1; }
		if($2) { $array[$a]{"invoiceNumber"} = $2."\n"; }
		if($3) { $array[$a]{"date"} = $3."\n"; }
		if($4) { $array[$a]{"bill"} = $4."\n"; }
		if($5) { $array[$a]{"filePath"} = $5."\n"; }
                    $a++;   #Increment counter
                                      } #End of match statement

	}#End of "Match" if statement

 for ($x = 626; $x < $a; $x++){

   #Split Invoice issue date for comparison if it exists
   if (exists($start[$x])){
   @startDate = split(/-/, $start[$x]);
   }

     #If the year is greater.
     if($year > $startDate[0]){

              #Output Invoice info    
              if (exists($array[$x])){               #Check to see if array position exists
                   %hash = %{$array[$x]};            #Make a Reference to the array
	      foreach $key (sort keys %hash)  {
	           print "$key=$hash{$key}";  }
                                     }      
                              }

       #If the year is equal and the month is less:
       elsif(($year == $startDate[0]) && ($mon > $startDate[1])){

                #Output Invoice info  
                if (exists($array[$x])){               #Check to see if array position exists
                     %hash = %{$array[$x]};            #Make a Reference to the array
	        foreach $key (sort keys %hash)  {
	             print "$key=$hash{$key}";  }
                                          }
	                             
                              }

         #If the year and month are equal and the day is less
         elsif(($year == $startDate[0]) && ($mon == $startDate[1]) && ($mday >= $startDate[2])){

                  #Output Invoice info  
                  if (exists($array[$x])){               #Check to see if array position exists
                       %hash = %{$array[$x]};            #Make a Reference to the array
	          foreach $key (sort keys %hash)  {
	               print "$key=$hash{$key}";  }
                                            }
	                             
      }   #End of for loop

} #End of while loop

if ($a == 626) {exit(626);}

close $file;

exit(5);
